import React, { useState, useRef, useEffect } from 'react';
import { useIntl, FormattedMessage } from 'umi';
import { PageContainer, FooterToolbar } from '@ant-design/pro-layout';
import ProTable from '@ant-design/pro-table';
import {getDOsByUser,deleteDo} from '@/services/doa/repo'
import { history, Link } from 'umi';
import {message} from "antd";


const DoManage = () => {


  const doRef = useRef();
  const [doi,setDoi] = useState("")
  const [doInfo,setDoInfo] = useState([])

  const info = (msg) => {
    message.info(msg);
  };


  /**
   * @en-US International configuration
   * @zh-CN 国际化配置
   * */

  const intl = useIntl();

  useEffect(()=>{
    getDOsByUser().then((response)=>{
      const doList = JSON.parse(response.data)
      console.log(doList)
      const dos = doList?.map((item, index) => {
        return {"doi":item}
      })
      setDoInfo(dos)
    })
  },[])

  useEffect(()=>{
    doRef.current.reloadAndRest()
  },[doInfo])

  const doColumns = [
    {
      title: '序号',
      dataIndex: 'index',
      valueType: 'indexBorder',
    },
    {
      dataIndex: 'doi',
      title: '标识',
      search: false,
    },
    {
      dataIndex: 'desc',
      title: '描述',
      search: false,
    },
    {
      title: '操作',
      valueType: 'option',
      width: 250,
      render: (text, record, _, action) => [
        <a
          key="detail"
          onClick={() => {
            history.push({pathname:"/doa/dodetail/resolve",query:{"doi":record.doi}})
          }}
        >
          详情
        </a>,
        <a
          key="delete"
          onClick={() => {
            deleteDo({doID:record.doi}).then((response) => {
              if(response?.success){
                if(response?.data === "success"){
                  setDoInfo(doInfo.filter((item) => item.doi !== record.doi));
                  info("删除成功！")
                }else{
                  info("删除失败！")
                }
              }
            })
          }}
        >
          删除
        </a>,
      ],
    },
  ];

  return (
    <PageContainer>
      <ProTable
        headerTitle={intl.formatMessage({
          id: 'pages.irsAdmin.doTable.title',
          defaultMessage: '数字对象列表',
        })}
        style={{"margin-bottom": "20px"}}
        actionRef={doRef}
        search={false}
        rowKey="key"
        options={{
          search: true,
        }}
        request={async (params, sort, filter) => {
          //{current: 1, pageSize: 20, keyword: "21221"}
          console.log(params,sort,filter)
          return Promise.resolve({
            data: doInfo,
            success: true,
          });
        }}
        columns={doColumns}
      />
    </PageContainer>
  );
};

export default DoManage;

